//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.滑动窗口.定长滑动窗口;

public class demo_1423 {
    public demo_1423() {
    }

    public int maxScore(int[] cardPoints, int k) {
        int n = cardPoints.length;
        if (k == n) {
            return cardPointsSum(cardPoints, 0, n);
        } else {
            int max = 0;

            for(int i = 0; i <= k; ++i) {
                int sum = cardPointsSum(cardPoints, 0, i) + cardPointsSum(cardPoints, n - k + i, n);
                max = Math.max(max, sum);
            }

            return max;
        }
    }

    public static int cardPointsSum(int[] cardPoints, int l, int r) {
        int sum = 0;

        for(int i = l; i < r; ++i) {
            sum += cardPoints[i];
        }

        return sum;
    }

    public int maxScore2(int[] cardPoints, int k) {
        int n = cardPoints.length;
        int lSum = 0;
        int rSum = 0;

        for(int i = 0; i < k; ++i) {
            lSum += cardPoints[i];
        }

        int max = lSum;
        int i = k - 1;

        for(int j = n - 1; i >= 0; --j) {
            lSum -= cardPoints[i];
            rSum += cardPoints[j];
            max = Math.max(max, rSum + lSum);
            --i;
        }

        return max;
    }
}
